package com.plz.afmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.plz.afmt.entity.EnterprisesEntity;
import com.plz.afmt.entity.SysUserEntity;
import com.plz.afmt.entity.TokenEntity;
import com.plz.afmt.service.EnterprisesService;
import com.plz.afmt.service.TokenService;
import com.plz.afmt.vo.BaseUserLoginVo;
import com.plz.common.exception.BusinessException;
import com.plz.shrio.utils.UserMangerUtil;
import com.plz.web.util.PageCommonUtil;
import com.plz.web.base.vo.PagerResult;
import com.plz.afmt.dto.UserDto;
import com.plz.afmt.dto.UserQueryDto;
import com.plz.afmt.entity.UserEntity;
import com.plz.afmt.mapper.UserMapper;
import com.plz.afmt.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plz.afmt.transfer.UserTransfer;
import com.plz.afmt.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * 用户表
 *
 * @author jzc
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
    @Autowired
    private UserTransfer userTransfer;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private EnterprisesService enterprisesService;
    @Autowired
    private LocalUserServiceImpl localUserService;

    @Override
    public PagerResult<UserVo> pageQuery(UserQueryDto dto) {
        QueryWrapper<UserEntity> queryWrapper = PageCommonUtil.dtoToWrapper(UserEntity.class, dto);
        queryWrapper.orderByDesc("id");
        PagerResult<UserEntity> entityPagerResult = PageCommonUtil.sigelPage(dto, queryWrapper, this::page);
        PagerResult<UserVo> voPagerResult = userTransfer.pagePo2PageVo(entityPagerResult);
        return voPagerResult;
    }

    @Override
    public UserEntity addOrUpdate(UserDto dto) {
        UserEntity entity = new UserEntity();
        BeanUtils.copyProperties(dto, entity);
        this.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public List<UserEntity> addOrUpdateList(List<UserDto> dto) {
        List<UserEntity> entitys = userTransfer.listDto2listPo(dto);
        this.saveOrUpdateBatch(entitys);
        return entitys;
    }

    @Override
    public List<UserVo> listFor(UserQueryDto dto) {
        QueryWrapper<UserEntity> queryWrapper = PageCommonUtil.dtoToWrapper(UserEntity.class, dto);
        List<UserEntity> list = this.list(queryWrapper);
        List<UserVo> vos = userTransfer.listPo2listVo(list);
        return vos;
    }

    @Override
    public UserVo detail(Long id) {
        UserEntity entity = this.getById(id);
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public UserEntity getByMobile(String phoneNumber) {
        return this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getMobile, phoneNumber));
    }

    @Override
    public UserEntity getUserByUnionId(String unionId) {
        return this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getWxUnionid, unionId));
    }

    @Override
    public String creatToken(UserEntity user) {
        BaseUserLoginVo result = new BaseUserLoginVo();
        result.setId(user.getId())
                .setLoginName(user.getMobile())
                .setUserName(user.getNickName())
                .setUserType(1);
        String token = localUserService.generateToken(result);
        return token;
    }

    @Override
    public List<UserEntity> listByEnterprisesCode(String enterprisesCode) {
        return this.list(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getEnterprisesCode, enterprisesCode));
    }

    @Override
    public UserVo me() {
        UserEntity userEntity = this.getById(UserMangerUtil.getUserId());
        UserVo userVo = userTransfer.po2vo(userEntity);
        if(StringUtils.isNotBlank(userVo.getEnterprisesCode())){
            EnterprisesEntity enterprisesEntity = enterprisesService.getByCode(userVo.getEnterprisesCode());
            userVo.setEnterprisesName(enterprisesEntity.getName());
        }
        return userVo;
    }

    @Override
    public Boolean binding(String code) {
        EnterprisesEntity enterprisesEntity = enterprisesService.getByCode(code);
        if(enterprisesEntity != null){
            UserEntity userEntity = this.getById(UserMangerUtil.getUserId());
            if(code.equals(userEntity.getEnterprisesCode())){
                throw new BusinessException("已绑定该企业，请勿重复操作！");
            }
            List<UserEntity> userEntities = this.listByEnterprisesCode(code);
            if(userEntities.size()>= enterprisesEntity.getMaxNum()){
                throw new BusinessException("该企业已达人数上线！");
            }
            userEntity.setEnterprisesCode(enterprisesEntity.getCode());
            userEntity.setVipStatus(1);
            userEntity.setLiberalTime(enterprisesEntity.getVipStartTime());
            userEntity.setExpiredTime(enterprisesEntity.getVipEndTime());
            return this.updateById(userEntity);
        }
        return false;
    }

    @Override
    public Boolean lifted(Long userId) {
        UserEntity userEntity = this.getById(userId);
        userEntity.setEnterprisesCode(null)
                .setVipStatus(0)
                .setExpiredTime(null)
                .setLiberalTime(null);
        return this.updateById(userEntity);
    }

    @Override
    public Boolean notice() {
        return this.update(new LambdaUpdateWrapper<UserEntity>()
                .eq(UserEntity::getId,UserMangerUtil.getUserId())
                .set(UserEntity::getNotice,1));
    }

}



